package com.yjn.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjn.bean.Result;
import com.yjn.exception.ParamsException;
import com.yjn.pojo.User;
import com.yjn.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @description: user controller
 * @author: Yujn
 * @create: 2021-02-18 15:30
 **/
@RequestMapping("user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 新增用户
     * @param user
     * @return
     * @author yujn
     * @date 2021/2/19 8:59
     */
    @PostMapping("/save")
    public Result save(){
        User user=new User();
        user.setUsername("码云");
        user.setPassword("232432");
        if (StrUtil.isEmpty(user.getUsername())){
            throw new ParamsException("用户名不允许为空");
        }
        if (StrUtil.isEmpty(user.getPassword())){
            throw new ParamsException("密码不允许为空");
        }
        boolean save = userService.save(user);
        return save?Result.success(save):Result.failure(save);
    }

    /**
     * 根据ID删除用户
     * @param id
     * @return
     * @author yujn
     * @date 2021/2/19 9:06
     */
    @PostMapping("/removeById")
    public Result removeById(Long id){
        if (id == null){
            throw new ParamsException("用户id不允许为空");
        }
        boolean b = userService.removeById(id);
        return b?Result.success(b):Result.failure(b);
    }

    /**
     * 根据ids批量删除用户
     * @param ids
     * @return
     * @author yujn
     * @date 2021/2/19 9:06
     */
    @PostMapping("/removeByIds")
    public Result removeById(List<Long> ids){
        if (CollUtil.isEmpty(ids)){
            throw new ParamsException("用户ids不允许为空");
        }
        boolean b = userService.removeByIds(ids);
        return b?Result.success(b):Result.failure(b);
    }

    /**
     * 根据ID更新用户
     * @param user
     * @return
     * @author yujn
     * @date 2021/2/19 9:20
     */
    @PostMapping("/updateById")
    public Result update(@RequestBody User user){
        if (user.getId2() == null){
            throw new ParamsException("用户id不允许为空");
        }
        if (StrUtil.isEmpty(user.getUsername())){
            throw new ParamsException("用户名不允许为空");
        }
        if (StrUtil.isEmpty(user.getPassword())){
            throw new ParamsException("密码不允许为空");
        }
        boolean b = userService.updateById(user);
        return b?Result.success(b):Result.failure(b);
    }

    /**
     * 根据主键查询用户
     * @param id
     * @return
     * @author yujn
     * @date 2021/2/18 15:36
     */
    @GetMapping("/getById")
    public Result<User> getById(Long id){
        if (id == null){
            throw new ParamsException("用户id不允许为空");
        }
        User byId = userService.getById(id);
        return Result.success(byId);
    }

    /**
     * 分页查询用户 支持多条件or搜索
     * @param user
     * @return
     * @author yujn
     * @date 2021/2/19 9:30
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer current,
                       @RequestParam(defaultValue = "10") Integer size,
                       User user){
        Page<User> page = new Page<>(current,size);
        IPage<User> resPage = userService.page(page, this.getQueryWrapper(user));
        return Result.success(resPage);
    }


    /**
     * 构造queryWrapper and
     * @param user
     * @return
     * @author yujn
     * @date 2021/2/19 9:27
     */
    public QueryWrapper<User> getQueryWrapper(User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        String username = user.getUsername();
        String password = user.getPassword();
        wrapper = wrapper.like(StrUtil.isNotEmpty(username),User.COL_USERNAME,username)
                .like(StrUtil.isNotEmpty(password),User.COL_PASSWORD,password);
        return wrapper;
    }



    /**
     * 根据主键查询用户
     * 测试xml共存
     * @param id
     * @return
     * @author yujn
     * @date 2021/2/18 15:36
     */
    @GetMapping("/xml/{id}")
    public Result<User> getByIdXml(@PathVariable Long id){
        User byId = userService.getByIdXml(id);
        byId.setRemark(byId.getRemark() + " 此实体通过xml查询出");
        return Result.success(byId);
    }

    /**
     * 根据主键查询用户
     * 测试xml共存
     * @param id
     * @return
     * @author yujn
     * @date 2021/2/18 15:36
     */
    @GetMapping("/anno/{id}")
    public Result<User> getByIdAnno(@PathVariable Long id){
        User byId = userService.getByIdAnno(id);
        byId.setRemark(byId.getRemark() + " 此实体通过Annotion查询出");
        return Result.success(byId);
    }
}
